import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: {
    data:[],
    per_page: 0,
    total: 0
  },
  Fields: [],
  itemDetail: {},
  mapLists: []
};
const actions = {
  //列表展示
  getLists({commit}, page) {
    if (!page) {
      commit("setLists", {
        data:[],
        per_page: 0,
        total: 0
      });
      return null
    }
    request.get(Server.action.YieldReport_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
          let listData = response.data.result.list;
          let spotData = response.data.result.spot;
          listData.chartData = {
            consumptionData: [],
            yieldData: [],
            averageData: [],
            vehicleData:[],
            date: []
          }
          spotData.forEach((item, i) => {
            // console.log(item);
            listData.chartData.consumptionData.push(item.consumption);
            listData.chartData.yieldData.push(item.yield);
            listData.chartData.averageData.push(item.unit_consumption);
            listData.chartData.date.push(item.statistics_date);
            listData.chartData.vehicleData.push(item.card_num);
          })
          commit("setLists", listData);
          let fields = response.data.result.fields;
          fields[0].width = 50
          fields.forEach(function (item, i) {
            fields[i].dataIndex = fields[i].key
            fields[i].align = 'center';
            if (item.key == 'statistics_date') item.width = 100
            if(i < fields.length-1) {
              fields[i].width = item.width || 80
            }
          })
          commit("setFields", fields);
        } else {

        }
    })
  },
  // 详情
  dataDetail({commit}) {
    request.get(Server.action.YieldReport_detail)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setDetail", response.data.result);
        // showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 设备地图展示
  mapLists({commit}) {
    request.get(Server.action.Map_list)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setMapLists", response.data.result.list);
      } else {
        showError( response.data.message )
      }
    })
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
  setDetail(state, data) {
      state.itemDetail = data;
  },
  setMapLists(state, lists) {
      state.mapLists = lists;
  },
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
